<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
        <link rel="stylesheet" href="../css/public.css" media="all">
        <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

        <style>
       
            .mycl{
               margin-left: 40px;
            }
        </style>
    </head>
<body style="background-color: #fff;">
<!-- 添加表单 -->
<div class="layui-form layuimini-form">
    
        <div class="layui-form-item">
            <label class="layui-form-label" style="">优惠卷图片</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <img id="pic" src='../images/优惠卷.png' style="width:160px;height:60px" />
                    <button type="button" class="layui-btn" id="browser-pic">
                        <i class="layui-icon">&#xe67c;</i>浏览图片
                    </button>
                </div>
            </div>
        </div>

        
        <div class="layui-form-item">
            <label class="layui-form-label">优惠卷类型</label>
            <div class="layui-input-block" >
                <select name="selecType" id="selec-type" lay-filter="typefilter" lay-search="">
                    <option value="">请选择类型</option>
                    <option value="1">折扣卷</option>
                    <option value="2">满减卷</option>
                    <option value="3">套餐卷</option>
                </select>
            </div>
        </div>


        <div class="layui-form-item" id="addCouponBlock">
            <label class="layui-form-label">优惠卷名称</label>
            <div class="layui-input-block" >
                <input type="text" id="add-Name"  name="addName" autocomplete="off" class="layui-input" />
            </div>
        </div>


        <div class="layui-form-item" id="couponsBlock" hidden>
            <label class="layui-form-label">套餐绑定</label>
            <div class="layui-input-block" >
                <select id="setMealCoupons" name="setMealCoupons"  lay-filter="type" lay-search="">
                    
               </select>
            </div>
        </div>
       

        <div class="layui-form-item">
            <label class="layui-form-label">说明</label>
            <div class="layui-input-block">
                <textarea id="add-Desc" name="addDesc"
                          placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item mycl" >
            <div class="layui-inline">
                <label class="layui-form-label">开始日期</label>
                <div class="layui-input-block layui-input-wrap">
                    <input type="text" class="layui-input" name="couponStartTime" id="ID-laydate-type-datetime-1" placeholder="选择开始日期">
                </div>
            </div>
        </div>

            
        <div class="layui-form-item mycl">
            <div class="layui-inline">
                <label class="layui-form-label">截止日期</label>
                <div class="layui-input-block layui-input-wrap">
                    <input type="text" class="layui-input" name="couponEndTime" id="ID-laydate-type-datetime-2" placeholder="选择开始日期">
                </div>
            </div>
        </div>


        <div class="layui-form-item mycl">
            <div class="layui-inline">
                <label class="layui-form-label" id="couponsReq">优惠力度</label>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="text" id="cpreferential" name="cpreferential" placeholder="￥" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>


        <div class="layui-form-item mycl">
            <div class="layui-inline">
                <label class="layui-form-label">满足条件</label>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="text" id="ccondition" name="ccondition" placeholder="￥" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">优惠卷状态</label>
            <div class="layui-input-block">
                <input type="radio" id="add-Status1" name="addStatus"
                       value="1" title="启用" checked>
                <input type="radio" id="add-Status2" name="addStatus"
                       value="0" title="停止">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否支持外送</label>
            <div class="layui-input-block">
                <input type="radio" id="add-Status1" name="goStatus"
                       value="1" title="支持" checked>
                <input type="radio" id="add-Status2" name="goStatus"
                       value="0" title="不支持">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否上架积分商城</label>
            <div class="layui-input-block">
                <input type="radio" id="add-Status1" name="supportItShop"
                       value="1" title="支持" checked lay-filter="enureSup">
                <input type="radio" id="add-Status2" name="supportItShop"
                       value="0" title="不支持" lay-filter="enureSup">
            </div>
        </div>

        <div class="layui-form-item"  id="exitBlock" >
            <label class="layui-form-label">所需积分兑换</label>
            <div class="layui-inline">
                    <input type="number" lay-filter="exinteger"  style="width: 100px;"value="0" id="Iexchange" name="Iexchange" placeholder="" autocomplete="off" class="layui-input" min="0" step="1" lay-affix="number" >
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    
</div>

<script>
        layui.use(['form', 'table','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            upload=layui.upload,
            laydate = layui.laydate,
            table = layui.table;
        var requrl= "http://localhost:8080/restaurant_web_war_exploded/";


 // 日期时间
 laydate.render({
            elem: '#ID-laydate-type-datetime-1',
            type: 'datetime',
            fullPanel: true // 2.8+
        });

 // 日期时间
 laydate.render({
            elem: '#ID-laydate-type-datetime-2',
            type: 'datetime',
            fullPanel: true // 2.8+
        });

       

        function showSetMeal(){
                        $.ajax({
                        type:"get",
                        url:"http://localhost:8080/restaurant_web_war_exploded/meal/listMeal",  
                        success:function (res){
                            let list=res.data.list;
                        if(res.data!=null){
                                var option="<option value=''>--请选择套餐--</option>";
                                for(var i=0;i<list.length;i++){
                                option+="<option value="+list[i].mealId+">"+list[i].mealName+"</option>";
                                //option+="<option value="+data[0][i].dId+">"+data[0][i].dName+"</option>";
                                }
                                $("#setMealCoupons").append(option);
                                form.render('select');
                        }    
                    }
                }) 
            }
        
        showSetMeal(); 
        //套餐显示     

       form.on('select(typefilter)',function(data){
            let couponsId=data.value; 
            if(couponsId==3){
                $("#couponsBlock").css("display","block");
                $("#couponsReq").text("出售价格");
            }else{
                $("#couponsBlock").css("display","none");
                $("#couponsReq").text("优惠力度");
            }
       })
       //优惠卷类型(select)监听

       form.on('select(type)',function(data){

       })

       form.on('radio(enureSup)',function(data){
            if(data.value==0){
                $("#exitBlock").prop("hidden",true);
            }else{     
                $("#exitBlock").prop("hidden",false);
            }
       })   
       //是否上架积分商城(radio)监听

       
        var myfile;     
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            
            if(myfile===null){
                layer.msg('请选择你要添加的优惠卷图',{icon:5,time:1000,shift:6});
				return false; 
            }   

            if(data.field.selecType===""){
                layer.msg('优惠卷类型不能为空',{icon:5,time:1000,shift:6});
				return false;	  
            }

            // 创建一个 FormData 实例  
            var formData = new FormData();  
            var newFormData  = new FormData();
          
            let mealid=data.field.setMealCoupons;
            if(mealid===""){
                formData.append('couponTypeId',data.field.selecType);
                formData.append('couponName',data.field.addName);
                formData.append('couponRemark',data.field.addDesc);
                formData.append('couponStartTime',data.field.couponStartTime);
                formData.append('couponEndTime',data.field.couponEndTime);
                formData.append('couponPreferential',data.field.cpreferential);
                formData.append('couponCondition',data.field.ccondition);
                formData.append('couponStatus',data.field.addStatus);
                formData.append('couponGoStatus',data.field.goStatus);
                formData.append('filePicture',myfile);
            }
            else{
                newFormData.append('mealId',data.field.setMealCoupons);
                newFormData.append('couponTypeId',data.field.selecType);
                newFormData.append('couponName',data.field.addName);
                newFormData.append('couponRemark',data.field.addDesc);
                newFormData.append('couponStartTime',data.field.couponStartTime);
                newFormData.append('couponEndTime',data.field.couponEndTime);
                newFormData.append('couponPreferential',data.field.cpreferential);
                newFormData.append('couponCondition',data.field.ccondition);
                newFormData.append('couponStatus',data.field.addStatus);
                newFormData.append('couponGoStatus',data.field.goStatus);
                newFormData.append('filePicture',myfile);
            }   

            $.ajax({
                type:"post",
                url:"http://localhost:8080/restaurant_web_war_exploded/coupons/insertCoupons",
                data:mealid===""?formData:newFormData,
                processData: false, // 告诉 jQuery 不要去处理发送的数据 
                contentType: false, // 告诉 jQuery 不要去设置 Content-Type 请求头  
                success:function (res){
                    var index= layui.layer.load(3, {  
                    shade: [1,'#fff']  })//0.1透明度的白色背景 
                    if(res.errorCode==200){
                        getCouponId();
                        var indexifr = parent.layer.getFrameIndex(window.name); // 获取当前 iframe 层的索引        
                        setTimeout(function(){
                        parent.layer.close(indexifr);
                        window.parent.postMessage('success', 'http://127.0.0.1:5500/index.html#/../html/couponsInfo.html'); // '*' 表示目标源，慎用，建议使用具体的源      
                        },1000)
                        
                        setTimeout(function(){   
                        layer.close(index);      
                        // 更新元素          
                        },1200);    
                    }
                }
            })

            
            function getCouponId(){
            let  cId;
            $.ajax({
                type:"get",
                url:"http://localhost:8080/restaurant_web_war_exploded/coupons/listCoupons",
                success:function (res){
                    if( res.errorCode==200){
                     let list=res.data.list;
                     cId= list[list.length-1].couponId; 
                     couponsAdd(cId);   
                    }
                }   
            })
           }
            //拿到优惠卷ID
            function couponsAdd(mycid){
                $.ajax({
                type:"post",
                url:"http://localhost:8080/restaurant_web_war_exploded/integral/insertIntegral",
                data:{couponId:mycid,integralExchange:data.field.Iexchange,integralState:1,integralSell:0},
                success:function (res){
                    // console.log(res);
                }
               })
            }//折扣卷上架
            //上架积分商城
            return false;
        });


        // 头像 
       //第三：使用组件
       let uploadInst = upload.render({
            // 绑定浏览文件元素
            elem: '#browser-pic',

            // 设置允许上传的文件类型
            accept: 'file',

            // 设置允许上传的文件后缀
            exts: 'jpg|png|jpeg|gif|webp',

            // 设计允许上传文件的大小，单位：KB
            size: 10 * 1024,

            // 设置不自动上传
            auto: false,    

            error: function (error) {			//上传失败的回调

            },
            choose: function (obj) {		//选择文件后的回调
                obj.preview(function (index, file, result) {
                    myfile=file;                     
                    //显示图片
                    $("#pic").prop("src", result);
                });
            }
        });
        
   })
</script>
</body>
</html>